Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #413, added methods TryMakeGenericType(s) #414

Closed

Conversation

SokyranTheDragon
Copy link
Member

The methods TryMakeGenericType/TryMakeGenericTypes will attempt to construct a generic type for a given type/collection of types. In case of non-generic types, or generic types that were already constructed they'll be returned as-is.

The method will attempt to find types that matches all generic constraints defined by the type itself, and if successful - call Type.MakeGenericType with the results.

In case we encounter a generic type with constraints no types can match an error will be displayed (unless ignoreErrors argument will be passed and set to true). The only exception is abstract classes with no subclasses, for which no error will be shown (assuming here they are unused).

The method was applied where needed. With RimWorld assembly itself, and majority of mods - this is going to do nothing. However, in case of mods that utilize generic types for the types that we end up patching - it should allow for those mods to properly work with Multiplayer without significantly breaking everything - for example, it should fix startup issues with Project Rimfactory.

As for error handling - I'm not 100% sure here. Perhaps adding Multiplayer.loadingErrors = true if errors are encountered? I can make changes where needed.

And finally - I've attempted to add XML documentation, as well as some comments to the code to explain it a bit more. However, I'm not completely satisfied with it... If there's any changes that should be made, let me know.

The methods `TryMakeGenericType`/`TryMakeGenericTypes` will attempt to construct a generic type for a given type/collection of types. In case of non-generic types, or generic types that were already constructed they'll be returned as-is.

The method will attempt to find types that matches all generic constraints defined by the type itself, and if successful - call `Type.MakeGenericType` with the results.

In case we encounter a generic type with constraints no types can match an error will be displayed (unless `ignoreErrors` argument will be passed and set to true). The only exception is abstract classes with no subclasses, for which no error will be shown (assuming here they are unused).

The method was applied where needed. With RimWorld assembly itself, and majority of mods - this is going to do nothing. However, in case of mods that utilize generic types for the types that we end up patching - it should allow for those mods to properly work with Multiplayer without significantly breaking everything - for example, it should fix startup issues with Project Rimfactory.

As for error handling - I'm not 100% sure here. Perhaps adding `Multiplayer.loadingErrors = true` if errors are encountered? I can make changes where needed.

And finally - I've attempted to add XML documentation, as well as some comments to the code to explain it a bit more. However, I'm not completely satisfied with it... If there's any changes that should be made, let me know.
@SokyranTheDragon SokyranTheDragon added fix Fixes for a bug or desync. 1.4 Fixes or bugs relating to 1.4 (Not Biotech). labels Jan 6, 2024
@Zetrith
Copy link
Member

Zetrith commented Jan 6, 2024

Generics and Harmony patches don't play nicely in general. Are you sure this works OK?

@SokyranTheDragon
Copy link
Member Author

Attempting to test it and checking Harmony documentation made me think stuff should be fine. However, testing was limited (due to not many mods using generic types for subtypes of vanilla types like Thing, Designator, etc.), and Harmony documentation on limitations with generics was a bit lacking.

I believe this may cause issues (depending on what mods do), so not fully safe... A different approach would be needed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.4 Fixes or bugs relating to 1.4 (Not Biotech). fix Fixes for a bug or desync.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants